package cn.fengwoo.springBoot.controller;


import cn.fengwoo.springBoot.entity.Result;
import cn.fengwoo.springBoot.entity.User;
import cn.fengwoo.springBoot.enums.ResultEnum;
import cn.fengwoo.springBoot.service.UserService;
import cn.fengwoo.springBoot.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户接口
 * Created by fangdaji on 2018/1/10.
 */
@RestController
@RequestMapping("/user")
@Api(value = "/UserApiController", description = "用户管理")
public class UserApiController {

    private final static Logger logger = LoggerFactory.getLogger(UserApiController.class);

    @Autowired
    private UserService userService;


    @ApiOperation(value="注册", notes="" )
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PostMapping("/register")
    public Result<User> register(User user){
        User register = userService.register(user);
        if(register!=null){
            logger.debug("【注册成功】:{}", register.toString());
           return ResultUtil.success(ResultEnum.SUCCESS,register);
        }

        logger.error("【注册失败】:{}", user.toString());
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }


    @ApiOperation(value="登录")
    @PostMapping("/login")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "path"),
//            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "path")
//    })
    public Result<User> login(@ApiParam("用户名") @RequestParam(value = "name", required = true) String name,
                              @ApiParam("密码") @RequestParam(value = "password", required = true) String password){
        User user = new User();
        user.setName(name);
        user.setPassword(password);

        User login = userService.login(user);
        if(login!=null){
            logger.debug("【登录成功】:{}", login.toString());
            return ResultUtil.success(ResultEnum.SUCCESS,login);
        }

        logger.error("【登录失败】:{}", user.toString());
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    @GetMapping("/list")
    public Result<List<User>> userList(){
        List<User> users = userService.userList();
        if(users!=null&&users.size()>0){
            return ResultUtil.success(ResultEnum.SUCCESS,users);
        }

        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    @PostMapping("/update")
    public Result<User> update(User user){
        User user1 = userService.changePassword(user);

        if(user1!=null){
            return ResultUtil.success(ResultEnum.SUCCESS,user1);
        }

        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }



}
